<?php

namespace App\Models\Mall;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Facades\Redis as Redis;

class MallGroup extends Model
{

    protected $table = 'mall_group';

    protected $primaryKey = 'id';

    public $timestamps = false;

    public $incrementing = false;

    public static function getCache( $id ) {

        $group_data = redisTransformation(Redis::get('WDH_MALL_GROUP_'.$id));

        if ( !empty( $group_data ) ) {
            return $group_data;
        }

        //查询门店组信息
        $group_data = self::find($id);
        if ( empty( $group_data ) ) {
            return null;
        }

        $data = [
            'id' => $group_data->id,
            'name' => $group_data->group_name,
            'map' => []
        ];

        //查询门店组映射表
        $group_map_data = MallGroupMap::where('group_id',$id)->get()->toArray();
        if ( !empty( $group_map_data ) ) {
            foreach ( $group_map_data as $item ){
                $data['map'][ $item['mall_id'] ] = 1;
            }
        }

        Redis::setex( 'WDH_MALL_GROUP_'.$group_data->id , 604800 , redisTransformation( $data , 'set' , 604800 ));

        return $data;

    }

}
